export const isElementInViewport = (el: HTMLElement, top: number = 0) => {
	if (!el) return false;
	const topPosition = el.getBoundingClientRect().top;
	// 如果工具栏的顶部位置小于等于0，则表示它已到达视口顶部
	return topPosition <= top;
};

// 截取账户 - 前4位 + 后4位
export const cutAccount = (account: string) => {
	return account.slice(0, 4) + '...' + account.slice(-4);
};

// 复制到剪贴板
export function copyToClipboardModern(text) {
	if (navigator.clipboard && window.isSecureContext) {
		// Navigator clipboard api method
		return navigator.clipboard.writeText(text).then(
			() => {
				console.log('Copied to clipboard successfully!');
			},
			(err) => {
				throw err;
			}
		);
	} else {
		// Fallback method for older browsers
		return copyToClipboard(text);
	}
}
function copyToClipboard(text) {
	// Create a temporary textarea element to hold the text
	const textarea = document.createElement('textarea');
	textarea.value = text;
	// Set the textarea to be out of the visible area
	textarea.style.position = 'fixed';
	textarea.style.top = '0';
	textarea.style.left = '0';
	textarea.style.width = '2em';
	textarea.style.height = '2em';
	textarea.style.padding = '0';
	textarea.style.border = 'none';
	textarea.style.outline = 'none';
	textarea.style.boxShadow = 'none';
	textarea.style.background = 'transparent';
	document.body.appendChild(textarea);
	// Select the text in the textarea
	textarea.select();
	// Execute the copy command
	try {
		const successful = document.execCommand('copy');
		const msg = successful ? 'successful' : 'unsuccessful';
		console.log('Copy command was ' + msg);
	} catch (err) {
		throw err;
	} finally {
		// Remove the temporary textarea element
		document.body.removeChild(textarea);
	}
}
